﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace M5.GeminiHybird.Core.Helper
{
    public static class AppSettingsReader<T>
    {
        
        //public static AppSettingsReader<T> Instance = new AppSettingsReader<T>();

        public static T AppSetting { get; set; }

        private static string _FilePath { get; } = Environment.CurrentDirectory + "/config/as";

        static AppSettingsReader()
        {

            if (!File.Exists(_FilePath))
            {
                throw new FileNotFoundException($"没有找到{_FilePath}配置文件, 请确认应用目录.");
            }
            
            StreamReader streamReader = new StreamReader(_FilePath);
            
            string json = streamReader.ReadToEnd();
            //Console.WriteLine(json);
            AppSetting = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            streamReader.Close();
        }


        public static void Save()
        {
            StreamWriter streamWriter = new StreamWriter(_FilePath);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(AppSetting);
            streamWriter.WriteLine(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
    }
}
